package cn.bj.xd.mdy.core;


import cn.bj.xd.mdy.entity.MdyControl;
import cn.bj.xd.mdy.entity.MdyData;
import cn.bj.xd.mdy.entity.MdyRequest;
import cn.bj.xd.mdy.entity.MdyResult;
import cn.bj.xd.mdy.model.RowInfo;
import cn.bj.xd.mdy.util.HttpUtil;
import cn.bj.xd.mdy.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 工作表客户端
 *
 * @author <a href="mailto:xd502djj@163.com">dongjunjie</a>
 * @date 2025-08-09 11:50
 */
@Slf4j
public class WorkSheetClient {
    private final String apiAddress;
    private final String apiKey;
    private final String sign;

    public WorkSheetClient(String apiAddress, String apiKey, String sign) {
        this.apiAddress = apiAddress;
        this.apiKey = apiKey;
        this.sign = sign;
    }

    /**
     * 添加工作表
     */
    public <T> MdyResult<T> addWorksheet(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/addWorksheet";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    public <T> MdyResult<T> addWorksheet(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/addWorksheet";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }


    /**
     * 获取筛选行
     */
    public <T> MdyResult<MdyData<T>> getFilterRows(MdyRequest request, TypeReference<MdyResult<MdyData<T>>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getFilterRows";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<MdyData<T>> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取筛选行
     */
    public <T> MdyResult<MdyData<T>> getFilterRows(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getFilterRows";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<MdyData<T>> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<MdyData<T>>>() {
        });
        return mdyResult;
    }


    /**
     * 添加行
     */
    public <T> MdyResult<T> addRow(RowInfo row, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/addRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(row.getWorkSheetId()).controls(row.getControls()).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 添加行
     */
    public <T> MdyResult<T> addRow(RowInfo row) {
        String url = apiAddress + "/v2/open/worksheet/addRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(row.getWorkSheetId()).controls(row.getControls()).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 添加多行
     */
    public <T> MdyResult<T> addRows(List<RowInfo> rows, Boolean returnRowIds, Boolean triggerWorkflow) {
        String url = apiAddress + "/v2/open/worksheet/addRows";
        List<List<MdyControl>> cRows = rows.stream().map(RowInfo::getControls).collect(Collectors.toList());
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(rows.get(0).getWorkSheetId()).returnRowIds(returnRowIds).triggerWorkflow(triggerWorkflow).rows(cRows).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取行
     */
    public <T> MdyResult<T> getRowById(String worksheetId, String rowId, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getRowById";
        HashMap<String, String> request = new HashMap<>();
        request.put("appKey", apiKey);
        request.put("sign", sign);
        request.put("rowId", rowId);
        request.put("worksheetId", worksheetId);
        request.put("returnControls", "true");
        String post = HttpUtil.get(url, null, null, request);
        MdyResult mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取行
     */
    public <T> MdyResult<T> getRowById(String worksheetId, String rowId) {
        String url = apiAddress + "/v2/open/worksheet/getRowById";
        HashMap<String, String> request = new HashMap<>();
        request.put("appKey", apiKey);
        request.put("sign", sign);
        request.put("rowId", rowId);
        request.put("worksheetId", worksheetId);
        request.put("returnControls", "true");
        String post = HttpUtil.get(url, null, null, request);
        MdyResult mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取行
     */
    public <T> MdyResult<T> getRowByIdPost(String worksheetId, String rowId, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getRowByIdPost";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(worksheetId).rowId(rowId).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取行
     */
    public <T> MdyResult<T> getRowByIdPost(String worksheetId, String rowId) {
        String url = apiAddress + "/v2/open/worksheet/getRowByIdPost";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(worksheetId).rowId(rowId).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 编辑行
     */
    public <T> MdyResult editRow(RowInfo row, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/editRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(row.getWorkSheetId()).rowId(row.getRowId()).controls(row.getControls()).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 编辑行
     */
    public <T> MdyResult<T> editRow(RowInfo row) {
        String url = apiAddress + "/v2/open/worksheet/editRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(row.getWorkSheetId()).rowId(row.getRowId()).controls(row.getControls()).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 批量编辑行
     */
    public <T> MdyResult editRows(String worksheetId, List<String> rowIds, List<MdyControl> controls, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/editRows";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(worksheetId).rowIds(rowIds).controls(controls).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 删除行
     */
    public <T> MdyResult<T> deleteRow(String worksheetId, String rowId, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/deleteRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(worksheetId).rowId(rowId).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 删除行
     */
    public <T> MdyResult<T> deleteRow(String worksheetId, String rowId) {
        String url = apiAddress + "/v2/open/worksheet/deleteRow";
        MdyRequest request = MdyRequest.builder().appKey(apiKey).sign(sign).worksheetId(worksheetId).rowId(rowId).build();
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取行关联
     */
    public <T> MdyResult<T> getRowRelations(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getRowRelations";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取行关联
     */
    public <T> MdyResult<T> getRowRelations(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getRowRelations";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取行分享链接
     */
    public <T> MdyResult<T> getRowShareLink(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getRowShareLink";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取行分享链接
     */
    public <T> MdyResult<T> getRowShareLink(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getRowShareLink";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取筛选行总数
     */
    public <T> MdyResult<T> getFilterRowsTotalNum(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getFilterRowsTotalNum";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取筛选行总数
     */
    public <T> MdyResult<T> getFilterRowsTotalNum(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getFilterRowsTotalNum";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取工作表讨论
     */
    public <T> MdyResult<T> getWorksheetDiscussions(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getWorksheetDiscussions";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取工作表讨论
     */
    public <T> MdyResult<T> getWorksheetDiscussions(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getWorksheetDiscussions";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }

    /**
     * 获取工作表操作日志
     */
    public <T> MdyResult<T> getWorksheetOperationLogs(MdyRequest request, TypeReference<MdyResult<T>> typeReference) {
        String url = apiAddress + "/v2/open/worksheet/getWorksheetOperationLogs";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, typeReference);
        return mdyResult;
    }

    /**
     * 获取工作表操作日志
     */
    public <T> MdyResult<T> getWorksheetOperationLogs(MdyRequest request) {
        String url = apiAddress + "/v2/open/worksheet/getWorksheetOperationLogs";
        request.setAppKey(apiKey);
        request.setSign(sign);
        String post = HttpUtil.post(url, new HashMap<>(), request);
        MdyResult<T> mdyResult = JsonUtil.toBean(post, new TypeReference<MdyResult<T>>() {
        });
        return mdyResult;
    }
}
